package cn.edu.swu.lms;

import cn.edu.swu.lms.db.BookResultsetVisitor;
import cn.edu.swu.lms.db.DatabaseService;
import cn.edu.swu.lms.db.ResultsetVisitor;
import cn.edu.swu.lms.entity.Book;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws SQLException {
        DatabaseService service = DatabaseService.getInstance();
        service.init();

        List<Book> books=service.query("select * from book",new BookResultsetVisitor());

        books.forEach(b-> {
            System.out.println(b.getAuthor());
        });

        List<String> names=service.query(
                "select name from books",
                new ResultsetVisitor() {

                    @Override
                    public List visit(ResultSet rs) throws SQLException {
                        List<String> names=new ArrayList<>();
                        while (rs.next()){
                            names.add(rs.getString("name"));

                    }
                        return names;
                }}
        );

        books.forEach(n-> {
            System.out.println(n);
        });
    }
}
